package com.guan.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.ruiji.mapper.CategoryMapper;
import com.guan.ruiji.pojo.Category;
import com.guan.ruiji.pojo.Dish;
import com.guan.ruiji.pojo.Setmeal;
import com.guan.ruiji.service.CategoryService;
import com.guan.ruiji.service.DishService;
import com.guan.ruiji.service.SetMealService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author guanzhenjie
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private DishService dishService;

    @Resource
    private SetMealService setMealService;

    @Override
    public boolean remove(Long id) {
        // 是否有关联菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int dishCount = dishService.count(dishLambdaQueryWrapper);
        if(dishCount > 0){
            return false;
        }
        // 是否有关联套餐
        LambdaQueryWrapper<Setmeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int setMealCount = setMealService.count(setMealLambdaQueryWrapper);
        if (setMealCount > 0){
            return false;
        }

        return super.removeById(id);
    }
}
